use "${clean}newspaper_analysis.dta", clear
keep if north == 1 | south == 1
drop if year >= 1900

// recoding Alexandria, VA to the South (technically part of DC until 1844)
replace south = 1 if city == "Alexandria"
replace north = 0 if city == "Alexandria"
replace state = "Virginia" if city == "Alexandria"

// dropping DC as a border area
drop if state == "District Of Columbia"

// standardizing variables
egen county_surban = std(county_urban_percent)
egen county_srugged = std(county_ruggedness)
egen county_spostoffice = std(county_postoffice_area)

// keeping the sample consistent
cap drop sample
gen sample = 1
foreach var of varlist $county_npcontrols {
	replace sample = . if `var' == .
}
keep if sample == 1


forvalues i = 3 / 6 {
	
preserve

local bw = `i' / 10
	

// predicting yhat for north and south
lowess singular year if north == 1, nograph bwidth(`bw') generate(yhat_n)
lowess singular year if south == 1, nograph bwidth(`bw') generate(yhat_s)


// collapsing
collapse (mean) yhat* singular (count) weight = singular, by(year north south)

cap drop weight_bubble
gen weight_bubble = .
replace weight_bubble = 1 if weight >= 1 & weight <= 10
replace weight_bubble = 2 if weight > 10 & weight <= 25
replace weight_bubble = 3 if weight > 25 & weight <= 50
replace weight_bubble = 4 if weight > 50



// taking the first differences of all of the lowess lines
sort north year
gen dys = yhat_s - yhat_s[_n-1]

sort south year
gen dyn = yhat_n - yhat_n[_n-1]



// graphing the lowess lines against the bubbles
twoway ///
	(scatteri 1 1860.5 1 1865.5, bcolor(gs10%25) recast(area)) ///
	(scatteri -.05 1860.5 -.05 1865.5, bcolor(gs10%25) recast(area)) ///
	(scatter singular year if north== 1 & weight_bubble == 1,  mcolor("${blue}") msym(Oh) msize(small) ) ///
	(scatter singular year if north== 1 & weight_bubble == 2,  mcolor("${blue}") msym(Oh) msize(medium) ) ///
	(scatter singular year if north== 1 & weight_bubble == 3,  mcolor("${blue}") msym(Oh) msize(large) ) ///
	(scatter singular year if north== 1 & weight_bubble == 4,  mcolor("${blue}") msym(Oh) msize(huge) ) ///
	(line yhat_n year, lwidth(thick) lpattern(solid) lcolor("${blue}")) ///
	, legend(off) ///
	ytitle("") ///
	subtitle("North - BW`i'0%", box bexpand bcolor(gs2) color(white) size(medlarge) ) ///
	xscale(off) ///
	ylab(, grid glcolor(gs7%50)) ///
	name(north_bubble, replace) nodraw

	
twoway ///
	(scatteri 1 1860.5 1 1865.5, bcolor(gs10%25) recast(area)) ///
	(scatteri -.05 1860.5 -.05 1865.5, bcolor(gs10%25) recast(area)) ///
	(scatter singular year if south== 1 & weight_bubble == 1,  mcolor("${red}") msym(Oh) msize(small) ) ///
	(scatter singular year if south== 1 & weight_bubble == 2,  mcolor("${red}") msym(Oh) msize(medium) ) ///
	(scatter singular year if south== 1 & weight_bubble == 3,  mcolor("${red}") msym(Oh) msize(large) ) ///
	(scatter singular year if south== 1 & weight_bubble == 4,  mcolor("${red}") msym(Oh) msize(huge) ) ///
	(line yhat_s year, lwidth(thick) lpattern(dash) lcolor("${red}")) ///
	, legend(off) /// 
	subtitle("South BW`i'0%", box bexpand bcolor(gs2) color(white) size(medlarge) ) ///
	ytitle("") ///
	xscale(off) ///
	ylab(, grid glcolor(gs7%50)) ///
	name(south_bubble, replace) nodraw	

	
// graphing the derivate lines against the year
twoway ///
	(scatteri .03 1860.5 .03 1865.5, bcolor(gs10%25) recast(area)) ///
	(scatteri -.01 1860.5 -.01 1865.5, bcolor(gs10%25) recast(area)) ///
	(line dyn year, lwidth(medthick) lpattern(solid) lcolor("${blue}")) ///
	(line dys year, lwidth(medthick) lpattern(dash) lcolor("${red}")) ///
	, legend(off) ///
	ytitle("") ///
	xlab(1800(20)1900) ///
	xtitle("") ///
	subtitle("Year-on-Year {&Delta}", box bexpand bcolor(gs2) color(white) size(medlarge) ) ///
	ylab(, grid glcolor(gs7%50)) ///
	graphregion(margin(l=-5)) ///
	name(deriv, replace) nodraw

graph combine north_bubble south_bubble deriv, col(1)  name(bw`i', replace) nodraw

restore
}


graph combine bw3 bw4 bw5 bw6, row(1) l2title("Perc. Singular", size(small)) b2title("Year of Publication", size(small))
graph export "${output}FigS1_np_ns_acceleration_bw.pdf", as(pdf) replace












